/*
 * edge.c
 *
 *  Created on: Nov 28, 2010
 *      Author: cumeo89
 */

#include "edge.h"
#include "node.h"
#include "glib/gmem.h"
#include <glib.h>

gunichar e_charAt(Edge *edge, glong i, Ucs4String **stringArr) {
	return stringArr[edge->stringIndex]->s[edge->start + i];
}

glong e_length(Edge *edge, glong i) {
	glong end = (edge->end < 0 ? i-1 : edge->end);
	return end - edge->start + 1;
}

Edge *e_new(int index, glong start, glong end) {
	return e_new_with_child(index, n_new(), start, end);
}

Edge *e_new_with_child(int index, struct SNode *child, glong start, glong end) {
	Edge *edge = g_new(Edge, 1);
	edge->stringIndex = index;
	edge->parent = NULL;
	edge->start = start;
	edge->end = end;
	edge->child = child;
	child->parentEdge = edge;
	return edge;
}

void e_free(Edge *edge) {
	if (edge->child->parentEdge == edge) {
		edge->child->parentEdge = NULL;
	}
	g_free(edge);
}

//Edge *e_split(Edge *edge, glong g) {
//	Node *v = edge->parent;
//	Edge *newEdge = e_new(edge->stringIndex, edge->start, edge->start+g-1);
//	n_addEdge(v, newEdge);
//	edge->start += g;
//	n_addEdge(newEdge->child, edge);
//	return newEdge;
//}

Edge *e_split(Edge *edge, glong g) {
	Node *w = edge->child;
	Edge *newEdge = e_new_with_child(edge->stringIndex, w, edge->start+g, edge->end);

	edge->end = edge->start + g - 1;
	edge->child = n_new();
	edge->child->parentEdge = edge;
	n_addEdge(edge->child, newEdge);

	return edge;
}

void e_set_child(Edge *edge, struct SNode *child) {
	///????
}
